package com.jzy.gulimall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzy.common.utils.PageUtils;
import com.jzy.common.utils.Query;
import com.jzy.gulimall.product.dao.AttrGroupDao;
import com.jzy.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.jzy.gulimall.product.entity.AttrEntity;
import com.jzy.gulimall.product.entity.AttrGroupEntity;
import com.jzy.gulimall.product.entity.ProductAttrValueEntity;
import com.jzy.gulimall.product.service.AttrAttrgroupRelationService;
import com.jzy.gulimall.product.service.AttrGroupService;
import com.jzy.gulimall.product.service.AttrService;
import com.jzy.gulimall.product.service.ProductAttrValueService;
import com.jzy.gulimall.product.vo.AttrGroupWithAttrVo;
import com.jzy.gulimall.product.vo.item.SkuItemVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrService attrService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Resource
    private ProductAttrValueService productAttrValueService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 列表查询 分页查询 模糊匹配
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(key)) {
            wrapper.and(wrap -> wrap.eq("attr_group_id", key)).or().like("attr_group_name", key);
        }
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }
    }

    /**
     * 发布商品 规格参数
     * 根据分类Id查出对应的分组 以及分组的所有属性
     */
    @Override
    public List<AttrGroupWithAttrVo> getAttrGroupAndAttrByCatId(Long catelogId) {
        //查出当前分类下的所有属性分组
        List<AttrGroupEntity> groupEntityList = this.list(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //查出每个属性分组的所有属性
        return groupEntityList.stream().map(group -> {
            AttrGroupWithAttrVo attrVo = new AttrGroupWithAttrVo();
            BeanUtil.copyProperties(group, attrVo);
            List<AttrEntity> attrRelationList = attrService.getAttrRelationList(attrVo.getAttrGroupId());
            attrVo.setAttrs(attrRelationList);
            return attrVo;
        }).collect(Collectors.toList());
    }

    /**
     * 详情页 查询sku,spu分组及属性信息
     * 根据spuId查出对应的分组 以及分组的所有属性
     */
    @Override
    public List<SkuItemVo.SpuItemAttrGroupVo> getAttrGroupAndAttrBySpuId(Long spuId, Long catalogId) {
        List<SkuItemVo.SpuItemAttrGroupVo> spuItemAttrGroupVos = new ArrayList<>();
        try {
            List<AttrGroupEntity> groupEntities = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catalogId));
            groupEntities.forEach(group->{
                SkuItemVo.SpuItemAttrGroupVo attrGroupVo = new SkuItemVo.SpuItemAttrGroupVo();
                //每一个分组
                attrGroupVo.setGroupName(group.getAttrGroupName());
                //分组属性关系表->属性id
                List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", group.getAttrGroupId()));
                List<Long> attrIds = relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(attrIds)) {
                    List<ProductAttrValueEntity> attrValueEntities = productAttrValueService.getAttrInfoByAttrIds(attrIds);
                    //每一个分组的所有属性信息
                    List<SkuItemVo.SpuBaseAttrVo> spuBaseAttrVos = new ArrayList<>();
                    attrValueEntities.forEach(item->{
                        //每一个属性
                        SkuItemVo.SpuBaseAttrVo spuBaseAttrVo = new SkuItemVo.SpuBaseAttrVo();
                        BeanUtil.copyProperties(item, spuBaseAttrVo);
                        spuBaseAttrVos.add(spuBaseAttrVo);
                    });
                    attrGroupVo.setAttrValues(spuBaseAttrVos);
                    spuItemAttrGroupVos.add(attrGroupVo);
                }
            });
            return spuItemAttrGroupVos;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}